package com.ymt.bpm.web.rest.mobile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.service.engine.task.TiService;
import com.ymt.bpm.service.mobile.MTaskService;
import com.ymt.bpm.util.AES;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.EncodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Johnny on 2017/11/6.
 */
@RestController
@RequestMapping("/mapi/v1/task")
public class MTaskResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MTaskService mts;

    @Autowired
    private TiService tis;

    @GetMapping("/todo/{tiIdEnc}")
    public JSONObject getTodo(HttpServletRequest req,
                               @RequestParam("timezoneOffset")int timezoneOffset,
                               @PathVariable(value="tiIdEnc")String tiIdEnc) {
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        String userId = (String)req.getAttribute(Const.JWT_USER);
        if (log.isDebugEnabled()) {
            log.debug("getTodo tenantId="+tenantId);
            log.debug("getTodo tiIdEnc="+tiIdEnc);
            log.debug("getTodo timezoneOffset="+timezoneOffset);
            log.debug("getTodo userId="+userId);
        }
        String tiId = AES.decrypt(tiIdEnc);
        return mts.getTodo(tenantId, tiId, timezoneOffset);
    }

    @GetMapping("/done/{tiIdEnc}")
    public JSONObject getDone(HttpServletRequest req,
                              @RequestParam("timezoneOffset")int timezoneOffset,
                              @PathVariable(value="tiIdEnc")String tiIdEnc) {
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        String userId = (String)req.getAttribute(Const.JWT_USER);
        if (log.isDebugEnabled()) {
            log.debug("getDone tenantId="+tenantId);
            log.debug("getDone tiIdEnc="+tiIdEnc);
            log.debug("getDone timezoneOffset="+timezoneOffset);
            log.debug("getDone userId="+userId);
        }
        String tiId = AES.decrypt(tiIdEnc);
        return mts.getDone(tenantId, tiId, timezoneOffset);
    }

    @GetMapping("/appl/{piEidEnc}")
    public JSONObject getAppl(HttpServletRequest req,
                              @RequestParam("status")String status,
                              @RequestParam("timezoneOffset")int timezoneOffset,
                              @PathVariable(value="piEidEnc")String piEidEnc) {
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        String userId = (String)req.getAttribute(Const.JWT_USER);
        if (log.isDebugEnabled()) {
            log.debug("getAppl tenantId="+tenantId);
            log.debug("getAppl piEidEnc="+piEidEnc);
            log.debug("getAppl timezoneOffset="+timezoneOffset);
            log.debug("getAppl userId="+userId);
        }
        String piEid = AES.decrypt(piEidEnc);
        return mts.getAppl(tenantId, piEid, status, timezoneOffset);
    }

    @GetMapping("/dones/{piIdEnc}")
    public JSONArray getDonesByPiId(HttpServletRequest req,
                                    @RequestParam("timezoneOffset")int timezoneOffset,
                                    @PathVariable(value="piIdEnc")String piIdEnc) {
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        String userId = (String)req.getAttribute(Const.JWT_USER);
        if (log.isDebugEnabled()) {
            log.debug("getDonesByPiId tenantId="+tenantId);
            log.debug("getDonesByPiId piIdEnc="+piIdEnc);
            log.debug("getDonesByPiId timezoneOffset="+timezoneOffset);
            log.debug("getDonesByPiId userId="+userId);
        }
        String piId = AES.decrypt(piIdEnc);
        return mts.getDonesByPiId(tenantId, piId, Const.LANG_ZH, timezoneOffset);
    }

    @GetMapping("/markRead/{tiIdEnc}")
    public JSONObject markRead(HttpServletRequest req,
                               @PathVariable(value="tiIdEnc")String tiIdEnc) {
        String userId = (String)req.getAttribute(Const.JWT_USER);
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("markRead tiIdEnc="+tiIdEnc);
            log.debug("markRead userId="+userId);
            log.debug("markRead tenantId="+tenantId);
        }
        String tiId = AES.decrypt(tiIdEnc);
        return mts.markRead(tenantId, tiId);
    }

    @GetMapping("/datas/{piEid}")
    public JSONObject getDataS(HttpServletRequest req,
                            @PathVariable(value="piEid")String piEidEnc,
                            @RequestParam("model")String model,
                            @RequestParam(value="lang", defaultValue=Const.LANG_ZH)String lang,
                            @RequestParam(value="order", defaultValue="ASC")String order,
                            @RequestParam(value="sort", defaultValue="ITEM_NO_")String sort,
                            @RequestParam("offset")int offset,
                            @RequestParam("limit")int limit,
                            @RequestParam("timezoneOffset")int timezoneOffset,
                            @RequestParam(value="search", required = false)String search) {
        //todo use jwt
        if (log.isDebugEnabled()) {
            log.debug("getDataS search=" + search);
        }
        search = EncodeUtil.decode(search);
        String piEid = AES.decrypt(piEidEnc);
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        String userId = (String)req.getAttribute(Const.JWT_USER);
        if (log.isDebugEnabled()) {
            log.debug("getDataS tenantId=" + tenantId);
            log.debug("getDataS model=" + model);
            log.debug("getDataS lang=" + lang);
            log.debug("getDataS userId=" + userId);
            log.debug("getDataS order=" + order);
            log.debug("getDataS sort=" + sort);
            log.debug("getDataS offset=" + offset);
            log.debug("getDataS limit=" + limit);
            log.debug("getDataS timezoneOffset=" + timezoneOffset);
            log.debug("getDataS search=" + search);
        }
        return mts.getDataS(tenantId, piEid, model, lang, order, sort, offset, limit, timezoneOffset, search);
    }


    // ===========================================================

    @PostMapping("/complete/{tiId}")
    public JSONObject complete(HttpServletRequest req,
                               @PathVariable("tiId")String tiIdEnc,
                               @RequestBody Map<String, String> map) {
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        String loginName = (String)req.getAttribute(Const.JWT_USER);
        String piEidEnc = map.get("piEid");
        String params = map.get("params");
        String datam = map.get("datam");
        String tiId = AES.decrypt(tiIdEnc);
        String piEid = AES.decrypt(piEidEnc);
        if (log.isDebugEnabled()) {
            log.debug("complete tenantId="+tenantId);
            log.debug("complete loginName="+loginName);
            log.debug("complete tiId="+tiId);
            log.debug("complete params="+params);
        }
        mts.saveDataM(tenantId, piEid, datam);
        return tis.complete(tenantId, loginName, tiId, JSONObject.parseObject(params));
    }

    @PostMapping("/return/{tiId}")
    public JSONObject returnTask(HttpServletRequest req,
                                 @PathVariable("tiId")String tiIdEnc,
                                 @RequestBody Map<String, String> map) {
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        String loginName = (String)req.getAttribute(Const.JWT_USER);
        String displayName = (String)req.getAttribute(Const.DISPLAYNAME);
        String piEidEnc = map.get("piEid");
        String params = map.get("params");
        String datam = map.get("datam");
        String tiId = AES.decrypt(tiIdEnc);
        String piEid = AES.decrypt(piEidEnc);
        if (log.isDebugEnabled()) {
            log.debug("returnTask tenantId="+tenantId);
            log.debug("returnTask loginName="+loginName);
            log.debug("returnTask tiId="+tiId);
            log.debug("returnTask params="+params);
        }
        mts.saveDataM(tenantId, piEid, datam);
        return tis.returnTask(tenantId, loginName, displayName, tiId,
                JSONObject.parseObject(params));
    }

    @PostMapping("/reassign/{tiId}")
    public JSONObject reassignTask(HttpServletRequest req,
                                   @PathVariable("tiId")String tiIdEnc,
                                   @RequestBody Map<String, String> map) {
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        String loginName = (String)req.getAttribute(Const.JWT_USER);
        String displayName = (String)req.getAttribute(Const.DISPLAYNAME);
        String assignTo = map.get("assignTo");
        String assignToName = map.get("assignToName");
        String comment = map.get("comment");
        String tiId = AES.decrypt(tiIdEnc);
        if (log.isDebugEnabled()) {
            log.debug("reassignTask tenantId="+tenantId);
            log.debug("reassignTask loginName="+loginName);
            log.debug("reassignTask displayName="+displayName);
            log.debug("reassignTask tiId="+tiId);
            log.debug("reassignTask assignTo="+assignTo);
            log.debug("reassignTask assignToName="+assignToName);
            log.debug("reassignTask comment="+comment);
        }
        return tis.reassignTask(tenantId, loginName, displayName, tiId, assignTo, assignToName, comment);
    }

    @PostMapping("/involve/{tiId}")
    public JSONObject involve(HttpServletRequest req,
                              @PathVariable("tiId")String tiIdEnc,
                              @RequestBody Map<String, String> map) {
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        String loginName = (String)req.getAttribute(Const.JWT_USER);
        String displayName = (String)req.getAttribute(Const.DISPLAYNAME);
        String tiId = AES.decrypt(tiIdEnc);
        String comment = map.get("comment");
        String involveType = map.get("involveType");
        String involvedUsers = map.get("involvedUsers");
        String paramsStr = map.get("paramsStr");
        if (log.isDebugEnabled()) {
            log.debug("involve tenantId="+tenantId);
            log.debug("involve loginName="+loginName);
            log.debug("involve displayName="+displayName);
            log.debug("involve tiId="+tiId);
            log.debug("involve comment="+comment);
            log.debug("involve involveType="+involveType);
            log.debug("involve involvedUsers="+involvedUsers);
            log.debug("involve paramsStr="+paramsStr);
        }
        return tis.involve(tenantId, loginName, displayName, tiId, comment,
                involveType, involvedUsers, JSONObject.parseObject(paramsStr));
    }

    @GetMapping("/defer/{tiId}")
    public JSONObject defer(HttpServletRequest req,
                            @PathVariable("tiId")String tiIdEnc) {
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        String loginName = (String)req.getAttribute(Const.JWT_USER);
        String tiId = AES.decrypt(tiIdEnc);
        if (log.isDebugEnabled()) {
            log.debug("defer tenantId="+tenantId);
            log.debug("defer loginName="+loginName);
            log.debug("defer tiId="+tiId);
        }
        return tis.defer(tenantId, loginName, tiId);
    }

    @GetMapping("/claim/{tiId}")
    public JSONObject claim(HttpServletRequest req,
                            @PathVariable("tiId")String tiIdEnc,
                            @RequestParam("piEid")String piEid,
                            @RequestParam("activityId")String activityId) {
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        String loginName = (String)req.getAttribute(Const.JWT_USER);
        String tiId = AES.decrypt(tiIdEnc);
        if (log.isDebugEnabled()) {
            log.debug("claim tenantId="+tenantId);
            log.debug("claim loginName="+loginName);
            log.debug("claim tiId="+tiId);
            log.debug("claim piEid="+piEid);
            log.debug("claim activityId="+activityId);
        }
        return tis.claim(tenantId, loginName, tiId, piEid, activityId);
    }

    @GetMapping("/unclaim/{tiId}")
    public JSONObject unclaim(HttpServletRequest req,
                              @PathVariable("tiId")String tiIdEnc,
                              @RequestParam("piEid")String piEid,
                              @RequestParam("activityId")String activityId) {
        String tenantId = (String)req.getAttribute(Const.TENANT_ID);
        String loginName = (String)req.getAttribute(Const.JWT_USER);
        String tiId = AES.decrypt(tiIdEnc);
        if (log.isDebugEnabled()) {
            log.debug("unclaim tenantId="+tenantId);
            log.debug("unclaim loginName="+loginName);
            log.debug("unclaim tiId="+tiId);
            log.debug("unclaim piEid="+piEid);
            log.debug("unclaim activityId="+activityId);
        }
        return tis.unclaim(tenantId, loginName, tiId, piEid, activityId);
    }

}
